<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8"/>
<title>Insert title here</title>
<link rel="stylesheet" type="text/css" href="../extjs/resources/css/ext-all.css" />
<script type="text/javascript" src="../extjs/ext-all.js"></script>
<script type="text/javascript" src="../extjs/ext-lang-zh_CN.js"></script>
<script>

Ext.onReady(function(){
	Ext.QuickTips.init();
	windowst.show();
	 
});

var store2 = Ext.data.JsonStore({
	fields : []	
});

	var fielmenu2=new Ext.menu.Menu({
		items:[{text:'销售订单转入'}]
	});
	var fielmenu3=new Ext.menu.Menu({
		items:[{text:'单况状态切换',handler:function(){
				windows3.show();
			}},
		       {text:'历史交易查询',handler:function(){
		    	   windows2.show();
			}},
		]
	});

var grids=Ext.create('Ext.grid.Panel',{
    
	 listeners:{
				containerdblclick: function(grid, e, eOpts){ //单击事件
	    						grid.getStore().add({'name':'12', 'dizhi':'','bianma':'','lianxi':''});
    						
    					}
			},plugins: [
        				Ext.create('Ext.grid.plugin.CellEditing', {
            				clicksToEdit: 1
        				})
    				],
	columns: [
				 {header: '（栏号）',
	                    		xtype: 'rownumberer',
	                    		dataIndex: 'lh', 
	                    		width: 50,
								sortable: false
        					},
					
        		{ header: '物料编号',  dataIndex: 'pordid', width: 100,},
       			 { header: '物料名称', dataIndex: 'pordname',width: 120,
        			editor: { xtype:'textfield',
				 	listeners:{
						focus:function(grid, e, eOpts){
								/* windows2.show(); */
								}
					}
				 } },
					 { header: '规格型号', dataIndex: 'pordsize' },
					 { header: '单位名称', dataIndex: 'stdunitengname' },
					 { header: '数量', dataIndex: 'shuliang' },
					 { header: '币别', dataIndex: 'currname' },
					 { header: '标准进价', dataIndex: 'biaozhunjinjia' },
					 { header: '标准进价金额', dataIndex: 'biaozhunjinjiajine' },
					 { header: '进价', dataIndex: 'jinjiajine' },
					 { header: '进价金额', dataIndex: 'jinjiajine' },
					 { header: '需求日期', dataIndex: 'xuqiuriqi' },
					 { header: '建议采购日期', dataIndex: 'jianyicaigouriqi' },
					 { header: '未采购量', dataIndex: 'weicaigouliang' },
					 { header: '分录备注', dataIndex: 'fenlubeizhu' },
					 { header: '来源单别', dataIndex: 'laiyuandanbie' },
					 { header: '来源单号', dataIndex: 'laiyuandanhao' }
   					 ],
   			 height: 130,
			 width:1971,
			 autoScroll:false,
})

var filterPanel = Ext.create('Ext.panel.Panel', {
    bodyPadding: 6,  // 避免Panel中的子元素紧邻边框
    width: 1985,
	height:170,
    items:[grids],
	bbar:[{xtype:"label",text:'总计:',width:90},
	{xtype:"textfield",disabled:true,width:90,margin:'0 180',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0 40 0 30',value:'0.00'},
	{xtype:"textfield",disabled:true,width:90,margin:'0 0 0 0',value:'0.00'}],
});

var  tableds=Ext.create('Ext.TabPanel',{
	width:"100%",
	height:230,
	autoScroll:true,
	bodyPadding: 5,
	items:[{title:'内容',autoScroll:true,items:[filterPanel]},{title:'备注',xtype:'textarea',name:'Remark',fieldLabel: '备注',labelWidth: 30,}],
});


var formsg=Ext.create('Ext.form.Panel', {
    bodyPadding: 5,
	height:'100%',
	layout:"column",
	baseCls:'x-plain',
	items:[{
		 xtype: 'textfield',
        name: 'salesname',
        id:'salesname',
		bodyPadding: 5,
        fieldLabel: '业务人员',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        name: 'maker',
        id:'maker',
        fieldLabel: '制单人员',
		style:'margin-left:19px',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        name: 'departname',
        id:'departname',
        fieldLabel: '所属部门',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
		 xtype: 'textfield',
        name: 'permitter',
        id:'permitter',
        fieldLabel: '复核人员',
		style:'margin-left:19px',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			}],
	});


var forms=Ext.create('Ext.form.Panel', {
    bodyPadding: 5,
	height:'100%',
	layout:"column",
	baseCls:'x-plain',
	items:[
	{
		xtype: 'textfield',
        name: 'billstylename',
        id:'billstylename',
        fieldLabel: '采购请购类型',
		width:250,
        labelWidth: 80,
        anchor: '100%',
			},{
		style:'margin-left:9px;color:blue',
		 xtype: 'datefield',
        name: 'billdate',
        id:'billdate',
        fieldLabel: '单据日期',
        format : 'Y-m-d',
		width:250,
        labelWidth: 70,
        anchor: '100%',
			},{
			style:'margin-left:9px',
		 xtype: 'textfield',
        name: 'billno',
        id:'billno',
		style:"color:blue;margin-left:9px",
        fieldLabel: '单据号码',
		width:241,
        labelWidth: 70,
        anchor: '100%',
			},{
	             	fieldLabel: '单况',
					xtype:'combo',
	             	name: 'billstatus',
	             	id:'billstatus',
	             	displayField: 'name',
					labelWidth:79,
					width:259,
    				valueField: 'abbr',
					store: Ext.create('Ext.data.Store', {
						fields: ['abbr', 'name'],
    					data : [
        					{"abbr":"未结案", "name":"未结案"},
        					{"abbr":"已结案", "name":"已结案"},
							{"abbr":"无效","name":"无效"}
							
					   ]
					}),
					value: '未结案',
        			typeAhead: true,
        			mode: 'local',
        			triggerAction: 'all',
        			selectOnFocus:true,
	         	},tableds,formsg
	]
	
});

var windows3=new  Ext.Window({
	width:360,
	height:300,
	title:"单选--销售订单",
	closable:true,
	resizable:false, //设置是否可以改变大小
	draggable:true,
	anchor: '100%',
	items:[],
	listeners: {
		activate : function() {
			
		}
	}	
});

var grids3=Ext.create('Ext.grid.Panel',{ 
	listeners:{
		'itemdblclick' : function(grid, rowIndex, e,i){
			var bino=grid.store.getAt(i).get('billno');
			alert(bino);
			querycgqgById(bino);
			windows3.hide();
		}  		
    },
	columns: [
        { header: '单据号码',  dataIndex: 'billno', width: 96},
        { header: '客户订单',  dataIndex: 'billstylename', width: 96},
   	],
   	store:store2,
   	width:349,
   	height: 240,
	autoScroll:true,	
})

var btn=Ext.create("Ext.Button", {
    text: "取回",
    width:70,
    height:24,
    listeners: { "click": function () {

    }
    },
    id: "btn",
});	

function querycgqgById(id){
	Ext.Ajax.request({
		url:"/cgqg/querycgqgById",
		params:{bino:id},
		method : "post",  
		success:function(data){
			var pp=Ext.JSON.decode(data.responseText);
			Ext.getCmp('billno').setValue(pp.billno);
			Ext.getCmp('billstylename').setValue(pp.billstylename);
			Ext.getCmp('billdate').setValue(pp.billdate);
			Ext.getCmp('billstatus').setValue(pp.billstatus);
			Ext.getCmp('salesname').setValue(pp.salesname);
			Ext.getCmp('departname').setValue(pp.departname);
			Ext.getCmp('maker').setValue(pp.maker);
			Ext.getCmp('permitter').setValue(pp.permitter);
			grids.getStore().removeAll();
			grids.getStore().add(pp.qgcbvo);
		},
		failure : function() {  
	    }  
	});
	
}

var grids2=Ext.create('Ext.grid.Panel',{ 
	id:'grids2',
	name:'grids2',
	listeners:{
		'itemdblclick' : function(grid, rowIndex, e,i){
			var bino=grid.store.getAt(i).get('billno');
			alert(bino);
			querycgqgById(bino);
			windows2.hide();
		}  		
    },
	columns: [
		{header: '（栏号）',
	     xtype: 'rownumberer',
	     dataIndex: 'grids2', 
	     width: 58,
		 sortable: false
        },
        { header: '采购请购单号',  dataIndex: 'billno', width: 96},
        { header: '采购请购类型',  dataIndex: 'billstylename', width: 96},
        { header: '单据日期',  dataIndex: 'billdate', width: 96},
   	],
   	store:store2,
   	width:349,
   	height: 240,
	autoScroll:true,	
})


var windows2=new  Ext.Window({
	width:360,
	height:300,
	title:"历史记录查询",
	closable:true,
	resizable:false, //设置是否可以改变大小
	draggable:true,
	anchor: '100%',
	items:[grids2,btn],
	listeners: {
		activate : function() {	
			Ext.Ajax.request({
				url:"/cgqg/queryAllcgqg",
				params :{},
				method : "post",
				async: false,
				success : function(data){
					var de=Ext.JSON.decode(data.responseText);
					grids2.getStore().loadData(de);
				},
				failure : function(){
					
				}
			});
    	}
	 } 
});

var maxBillNo=0;//初始最大页数，随上一页下一页递增递减。
var BillNo=0;//窗口初始化时等于maxBillNo，用于做判断，
var shzt;//审核状态
//winform窗口
	var windowst=new  Ext.Window({
		width:590,
		height:430,
		title:"采购请购单",
		closable:true,
		resizable:false, //设置是否可以改变大小
		draggable:true,
		anchor: '100%',
		items:[forms],
		listeners : {
			activate : function() {
				selectCountBillno();
				findAllQG();
				/* alert("111"+maxBillNo); */
			}
		},
		bbar:[{xtype:"splitbutton",text:'转单',width:90,menu:fielmenu2},
		{xtype:"splitbutton",text:'功能',width:90,menu:fielmenu3},
		{xtype:"button",name:'baocun',id:'baocun',
			text:'保存',handler: function () {
				savecgqg();
				Ext.getCmp("billstylename").setDisabled(true);
				Ext.getCmp("billdate").setDisabled(true);
				Ext.getCmp("billno").setDisabled(true);
				Ext.getCmp("billstatus").setDisabled(true);
			}
	    },
	    {xtype:"button",name:'shanchu',id:'shanchu',
			text:'删除',handler: function () {
				delcgqg();
			}
	    },
	    {xtype:"button",name:'shangyiye',id:'shangyiye',
			text:'上一页',handler: function () {
				shangyiye();
			}
	    },
	    {xtype:"button",name:'xiayiye',id:'xiayiye',disabled : true,  
			text:'下一页',handler: function () {
				xiayiye();
			}
	    },
	    {xtype:"button",name:'shenhe',id:'shenhe',
			text:'审核',handler: function () {
				if(shzt=='已审核'){
					alert('已审核，不能重复审核!');
				}else{
					shzt='已审核';
					updateqgshzt();
					Ext.getCmp('shenhe').disable();
					Ext.getCmp('fanshenhe').enable();
					Ext.getCmp('shanchu').disable();
				}
				
			}
	    },
	    {xtype:"button",name:'fanshenhe',id:'fanshenhe',
			text:'反审核',handler: function () {
				if(shzt=='未审核'){
					alert('未审核，无法反审核!');
				}else{
					shzt='未审核';
					updateqgshzt();
					Ext.getCmp('shenhe').enable();
					Ext.getCmp('fanshenhe').disable();
				}
			}
	    },
	    {xtype:"button",name:'xiugai',id:'xiugai',
			text:'修改',handler: function () {
				Ext.getCmp("billstylename").setDisabled(false);
				Ext.getCmp("billdate").setDisabled(false);
				Ext.getCmp("billno").setDisabled(false);
				Ext.getCmp("billstatus").setDisabled(false);
				Ext.getCmp('baocun').enable();
			}
	    },
	    
		],
	});
	
	function shangyiye(){
		if(maxBillNo==0){
			Ext.getCmp('shangyiye').disable();
			/* alert('这已经是第一页了！'); */
		}else{
			/* alert("上一页"); */
			Ext.getCmp('xiayiye').enable();
			maxBillNo--;
			findAllQG();
		}
	}
	
	function xiayiye(){
		if(maxBillNo==BillNo){
			Ext.getCmp('xiayiye').disable();
			/* alert('这已经是最后一页了！'); */
		}else{
			/* alert("下一页"); */
			Ext.getCmp('shangyiye').enable();
			maxBillNo++;
			findAllQG();
		}
	}
	
	function updateqgshzt(){
		var billno=Ext.getCmp('billno').getValue();
		/* alert(shzt); */
		Ext.Ajax.request({
			url:"/cgqg/updateshzt",
			params :{
				'shzt':shzt,
				'billno':billno
			},
			method : "post",
			async: false,
			success : function(data){
				
			},
			failure : function(){
				
			}
		});
	};
	
	function savecgqg(){
        var det =Ext.pluck(grids.getStore().data.items,"data");
     	var  cgqg={
 				
     		billstylename:Ext.getCmp('billstylename').getValue(),				
     		billdate:Ext.getCmp('billdate').getRawValue(),
     		billno:Ext.getCmp('billno').getValue(),
     		billstatus:Ext.getCmp('billstatus').getValue(),		
     		salesname:Ext.getCmp('salesname').getValue(),
     		maker:Ext.getCmp('maker').getValue(),
     		departname:Ext.getCmp('departname').getValue(),
     		permitter:Ext.getCmp('permitter').getValue(),
     		shenhezhuangtai:'未审核',
     		curcurrencyname:'人民币',
     		qgcb:det
     	}
     	Ext.Ajax.request({
     		url : "/cgqg/save",
     		params :JSON.stringify(cgqg),
     		method : "post",
     		success : function(data) {
     			alert("保存成功");
     			Ext.getCmp('shenhe').enable();
     		},
     		headers: {'Content-Type':'application/json'},
     		failure : function() {
     			alert("信息提示", "信息删除出错，请联系管理员!");
     		}
     	});
		
	};
	
	function delcgqg(){
		var billno=Ext.getCmp('billno').getValue();
		Ext.Ajax.request({
			url:"/cgqg/delcgqgByid?billno="+billno,
			method : "post",
			success:function(){
				alert("删除成功");
				if(maxBillNo==0){
					Ext.getCmp('billno').setValue("");
					Ext.getCmp('billstylename').setValue("");
					Ext.getCmp('billdate').setValue(new Date());
					Ext.getCmp('billstatus').setValue("");
					Ext.getCmp('salesname').setValue("");
					Ext.getCmp('departname').setValue("");
					Ext.getCmp('maker').setValue("");
					Ext.getCmp('permitter').setValue("");
					grids.getStore().removeAll();
				}else{
					/* window.location.reload(); */
					maxBillNo--;
					findAllQG();
				}
			},
			headers: {'Content-Type':'application/json'},
     		failure : function() {
     			Ext.MessageBox.alert("信息提示", "信息删除出错，请联系管理员!");
     		}
			
		});
	};
	
	function selectCountBillno(){
		Ext.Ajax.request({
			url : "/cgqg/selectCountBillno",
			async: false,
			method : "get",
			success : function(result,request) {
				maxBillNo =Ext.decode(result.responseText);
				maxBillNo=parseInt(maxBillNo)-1;
				BillNo=maxBillNo;
			},
			failure : function(response) {
			}
		});
		
	};
	
	function findAllQG(){
			Ext.Ajax.request({
				url : "/cgqg/cgqgqueryById"/* ?pagenum="+maxBillNo */,
				params :{
					'id':maxBillNo
					},
				async: false,
				method : "post",
				success : function(data) {
				var pp=Ext.JSON.decode(data.responseText);
					Ext.getCmp('billno').setValue(pp.billno);
					Ext.getCmp('billstylename').setValue(pp.billstylename);
					Ext.getCmp('billdate').setValue(pp.billdate);
					Ext.getCmp('billstatus').setValue(pp.billstatus);
					Ext.getCmp('salesname').setValue(pp.salesname);
					Ext.getCmp('departname').setValue(pp.departname);
					Ext.getCmp('maker').setValue(pp.maker);
					Ext.getCmp('permitter').setValue(pp.permitter);
					grids.getStore().removeAll();
					grids.getStore().add(pp.qgcbvo);
					Ext.getCmp("maker").setDisabled(true);
					Ext.getCmp("permitter").setDisabled(true);
					Ext.getCmp('baocun').disable();
					selectQgshzt();
				},
				failure : function() {
					/* alert('数据错误'); */
				}
			});
	};
	function selectQgshzt(){
		var billno=Ext.getCmp('billno').getValue();
		Ext.Ajax.request({
			url : "/cgqg/selectQgshzt",
			params :{
				'billno':billno
				},
			method : "post",
			async: false,
			success : function(result,request) {
				var qgshzt=result.responseText;
				if(qgshzt=='未审核'){
					Ext.getCmp('fanshenhe').disable();
					Ext.getCmp('shenhe').enable();
					Ext.getCmp('shanchu').enable();
					Ext.getCmp('xiugai').enable();
				}
				if(qgshzt=='已审核'){
					Ext.getCmp('fanshenhe').enable();
					Ext.getCmp('shenhe').disable();
					Ext.getCmp('shanchu').disable();
					Ext.getCmp('xiugai').disable();
				}
					
			},
			failure : function() {
			}
		});
	};

</script>


</head>
<body>
	
</body>
</html>